package br.com.hotel.dao;

import java.util.ArrayList; 
import java.util.Date;
import java.util.List;

import org.hibernate.annotations.NamedQuery;

import br.com.hotel.entity.Apartamento;
import br.com.hotel.entity.Estadia;
import br.com.hotel.entity.Preco;

	
	

public class EstadiaDAO extends GenericJpaRepository<Estadia, Long>{

	public List<Estadia> estadias;
	public EstadiaDAO() throws ClassNotFoundException{
		createEntityManager();
	}
	
	public List<Estadia> getEstadias(){
		
		return findAll();
	}
	
	public List<Estadia> getEstadiaDisponivelCheckin(){
		javax.persistence.Query query = getEntityManager().createQuery("SELECT  p FROM Estadia p WHERE p.data_inicial=?1 AND (p.tipo=0 OR p.tipo=3)");
		query.setParameter(1, new Date());
		return query.getResultList();
	}
	
	public List<Estadia> getEstadiaAtivas(){
		javax.persistence.Query query = getEntityManager().createQuery("SELECT  p FROM Estadia p WHERE p.tipo=1");
		return query.getResultList();
	}

	public List<Estadia> getEstadiaCheckouted() {
		javax.persistence.Query query = getEntityManager().createQuery("SELECT  p FROM Estadia p WHERE p.tipo=1 OR p.tipo=2");
		return query.getResultList();
	}
	public List<Estadia> getApartamentoOcupado(){
		javax.persistence.Query query = getEntityManager().createQuery("SELECT  count(*) as ocupado FROM Estadia p WHERE (p.data_inicial<=?1 AND p.data_final>=?1)" +
				"AND (p.tipo!=1 OR p.tipo!=3) AND p.diaria_id = ?2");
		query.setParameter(1, new Date());
		return query.getResultList();
	}
	
	
	public Double getTotalServiceEstadia(Long estadiaId){
		javax.persistence.Query query = getEntityManager().createQuery("SELECT  SUM(p.preco*p.quantidade) as v FROM ServiceEstadia p WHERE p.estadia.Id="+estadiaId);
		
		Double d = (Double) query.getSingleResult();
		if(d==null)
			d=0.00;
		return d;
	}
	
	
}